package com.qidian.mespro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mespro.mapper.DictDataMapper;
import com.qidian.mespro.mapper.ProcessMapper;
import com.qidian.mespro.mapper.WorkflowProcessMapper;
import com.qidian.mespro.service.WorkflowProcessService;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProcess;
import org.springframework.beans.factory.annotation.Autowired;
import com.qidianit.mes.pojo.pro.Process;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author: DuYaTao
 * @ Date: 2025/9/19 - 14:09
 */
@Service
public class WorkflowProcessServiceImpl implements WorkflowProcessService {

    @Autowired
    private WorkflowProcessMapper workflowProcessMapper;

    @Autowired
    private ProcessMapper processMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Override
    public Result<PageInfo<WorkflowProcess>> getList(WorkflowProcess workflowProcess, int page, int size) {
        PageHelper.startPage(page, size);
        List<WorkflowProcess> list = workflowProcessMapper.selectList(workflowProcess);
        for (WorkflowProcess wp : list) {
            //设置当前工序
            wp.setProcess(processMapper.selectProcessById(wp.getProcessId()));
            // 查询当前工艺的所有工序 按照序号顺序排序。  根据当前工序找到当前工序的 下一道工序。
            // 查询当前工序的所有工序
            List<Process> processes = processMapper.selectProcessByWorkflowId(wp.getWorkflowId());
            Process nextProcess = getNextProcess(processes, wp.getProcess());
            wp.setNextProcess(nextProcess);
            // 查询与下一道工序关系的字典信息
            wp.setRelationNextDict(dictDataMapper.selectById(wp.getRelationNext()));
        }
        return Result.success(new PageInfo<>(list));

    }

    @Override
    public Result<WorkflowProcess> addWorkflowProcess(WorkflowProcess workflowProcess) {

        Long id = workflowProcess.getProcessId();
        List<Process> processes = processMapper.selectProcessByWorkflowId(workflowProcess.getWorkflowId());
        if (id > processes.size()) {
            Result.error("序号"+ id + "已存在");
        }

        WorkflowProcess newWorkflowProcess = workflowProcessMapper.selectByWorkflowIdAndProcessId(workflowProcess.getWorkflowId(), workflowProcess.getProcessId());
        if (newWorkflowProcess != null) {
            return Result.error("工序"+ processMapper.selectProcessById(newWorkflowProcess.getProcessId()).getProcessName()+ "已存在");
        }

//        List<Process> processes = processMapper.selectProcessByWorkflowId(workflowProcess.getWorkflowId());
//        Long id = workflowProcess.getProcessId();
//        for (Process process : processes) {
//            Long id1 = Long.valueOf(process.getId());
//            if (id1.equals(id)) {
//                return Result.error("工序"+ process.getProcessName()+ "已存在");
//            }
//        }
        return workflowProcessMapper.insert(workflowProcess) == 1 ? Result.success() : Result.error("添加失败");
    }

    @Override
    public Result<WorkflowProcess> selectById(Long id) {
        //将工艺工序数据查出来
        WorkflowProcess workflowProcess = workflowProcessMapper.selectById(id);
        //数据为空直接返回null
        if (workflowProcess == null) {
            return null;
        }
        //再设置工艺工序 中 process 数据
        workflowProcess.setProcess(processMapper.selectProcessById(workflowProcess.getProcessId()));
        return Result.success(workflowProcess);
    }

    @Override
    public Result<WorkflowProcess> updateWorkflowProcess(WorkflowProcess workflowProcess) {
        return workflowProcessMapper.updateWorkflowProcess(workflowProcess) == 1 ? Result.success(): Result.error("修改失败");
    }

    @Override
    public Result<WorkflowProcess> deleteById(Long id) {
        return workflowProcessMapper.deleteById(id) == 1 ? Result.success(): Result.error("删除失败");
    }

    @Override
    public Result<WorkflowProcess> deleteBatchByIds(Long[] ids) {
        return workflowProcessMapper.batchDeleteByIds(ids) > 0 ? Result.success(): Result.error("批量删除失败");
    }

    /**
     *
     * @param processes
     * @param process
     * @return
     */
    private Process getNextProcess(List<Process> processes, Process process) {
        if (processes == null || processes.isEmpty()) {
            return null;
        }
        for (int i = 0; i < processes.size() - 1 ; i++) {
            if (processes.get(i).getId().equals(process.getId()) && processes.get(i + 1) != null) {
                return processes.get(i + 1);
            }
        }
        return null;
    }
}
